#include <sdk.h>
#include <wx/listbook.h>
#include "configurationpanel.h"
#include "compilersettingsdlg.h"

BEGIN_EVENT_TABLE( CompilerSettingsDlg, wxScrollingDialog )
END_EVENT_TABLE()

CompilerSettingsDlg::CompilerSettingsDlg( wxWindow* parent ) {
  wxXmlResource* xml_res = wxXmlResource::Get();
  wxString resPath = ConfigManager::GetDataFolder() + _T( "/配置编译器.xrc" ) ;
  if( !xml_res->Load( resPath ) )  {
    xml_File_lock = false;
    Manager::Get()->GetLogManager()->LogInf( F( _T( "文件丢失: %s " ), resPath.wx_str() ) );
    return;
  }
  if( !xml_res->LoadObject( this, parent, _T( "dlgCompilerSettings" ), _T( "wxScrollingDialog" ) ) ) {
    xml_File_lock = false;
    Manager::Get()->GetLogManager()->LogInf( _T( "资源丢失" ) );
    return;
  }
  xml_File_lock = true;
  XRCCTRL( *this, "wxID_OK", wxButton )->SetDefault();
  m_pImageList = new wxImageList( 80, 80 );
  Connect( XRCID( "nbMain" ), wxEVT_COMMAND_LISTBOOK_PAGE_CHANGING, wxListbookEventHandler( CompilerSettingsDlg::OnPageChanging ) );
  Connect( XRCID( "nbMain" ), wxEVT_COMMAND_LISTBOOK_PAGE_CHANGED, wxListbookEventHandler( CompilerSettingsDlg::OnPageChanged ) );
  XRCCTRL( *this, "txtBatchBuildsCmdLine", wxTextCtrl )->Enable( false );
  const wxArrayString &bbplugins = cbReadBatchBuildPlugins();
  wxCheckListBox* clb = XRCCTRL( *this, "chkBBPlugins", wxCheckListBox );
  clb->Clear();
  clb->SetMinSize( wxSize( -1, 150 ) );
  const PluginElementsArray& plugins = Manager::Get()->GetPluginManager()->GetPlugins();
  for( size_t i = 0; i < plugins.GetCount(); ++i ) {
    PluginElement* elem = plugins[i];
    if( !elem ) {
      continue;
    }
    M_Code_Plugin* plugin = elem->plugin;
    if( !plugin || !plugin->IsAttached() ) {
      continue;
    }
    wxString filename = wxFileName( elem->fileName ).GetFullName();
    size_t index = clb->Append( elem->info.title );
    for( size_t n = 0; n < bbplugins.GetCount(); ++n ) {
      if( filename.CmpNoCase( bbplugins[n] ) == 0 ) {
        clb->Check( index, plugin->IsAttached() );
        break;
      }
    }
  }
  AddPluginPanels();
  GetSizer()->SetSizeHints( this );
  CentreOnParent();
  Layout();
}

CompilerSettingsDlg::~CompilerSettingsDlg() {
  delete m_pImageList;
}

void CompilerSettingsDlg::AddPluginPanels() {
  wxListbook* lb = XRCCTRL( *this, "nbMain", wxListbook );
  wxWindow* existingPage = lb->GetPage( 0 );
  wxString existingTitle = lb->GetPageText( 0 );
  lb->RemovePage( 0 );
  ConfigurationPanelsArray local;
  Manager::Get()->GetPluginManager()->GetConfigurationPanels( cgCompiler, lb, local );
  for( size_t i = 0; i < local.GetCount(); ++i ) {
    cbConfigurationPanel* panel = local[i];
    panel->SetParentDialog( this );
    lb->AddPage( panel, panel->GetTitle() );
    wxString onFile = ConfigManager::GetDataFolder() + _T( "/images/settings/" ) + panel->GetBitmapBaseName() + _T( ".png" );
    wxString offFile = ConfigManager::GetDataFolder() + _T( "/images/settings/" ) + panel->GetBitmapBaseName() + _T( "-off.png" );
    m_pImageList->Add( M_Code_LoadBitmap( onFile, wxBITMAP_TYPE_PNG ) );
    m_pImageList->Add( M_Code_LoadBitmap( offFile, wxBITMAP_TYPE_PNG ) );
    lb->SetPageImage( lb->GetPageCount() - 1, m_pImageList->GetImageCount() - 2 );
    m_PluginPanels.Add( panel );
  }
  lb->AddPage( existingPage, existingTitle );
  wxString onFile = ConfigManager::GetDataFolder() + _T( "/images/settings/batch.png" );
  wxString offFile = ConfigManager::GetDataFolder() + _T( "/images/settings/batch-off.png" );
  m_pImageList->Add( M_Code_LoadBitmap( onFile, wxBITMAP_TYPE_PNG ) );
  m_pImageList->Add( M_Code_LoadBitmap( offFile, wxBITMAP_TYPE_PNG ) );
  lb->SetPageImage( lb->GetPageCount() - 1, m_pImageList->GetImageCount() - 2 );
  UpdateListbookImages();
}

void CompilerSettingsDlg::UpdateListbookImages() {
  wxListbook* lb = XRCCTRL( *this, "nbMain", wxListbook );
  int sel = lb->GetSelection();
  if( SettingsIconsStyle( Manager::Get()->GetConfigManager( _T( "app" ) )->ReadInt( _T( "/environment/settings_size" ), 0 ) ) ) {
    SetSettingsIconsStyle( lb->GetListView(), sisNoIcons );
    lb->SetImageList( nullptr );
  } else {
    lb->SetImageList( m_pImageList );
    for( size_t i = 0; i < 1 + m_PluginPanels.GetCount(); ++i ) {
      lb->SetPageImage( i, ( i * 2 ) + ( sel == ( int )i ? 0 : 1 ) );
    }
    SetSettingsIconsStyle( lb->GetListView(), sisLargeIcons );
  }
  wxString label = lb->GetPageText( sel );
  while( label.Replace( _T( " & " ), _T( " && " ) ) ) ;
  XRCCTRL( *this, "lblBigTitle", wxStaticText )->SetLabel( label );
  XRCCTRL( *this, "pnlTitleInfo", wxPanel )->Layout();
}

void CompilerSettingsDlg::OnPageChanging( M_Code_unused wxListbookEvent& event ) {
}

void CompilerSettingsDlg::OnPageChanged( wxListbookEvent& event ) {
  if( event.GetOldSelection() != -1 && event.GetSelection() != -1 ) {
    UpdateListbookImages();
  }
}

void CompilerSettingsDlg::EndModal( int retCode ) {
  if( retCode == wxID_OK ) {
    wxArrayString bbplugins;
    wxCheckListBox* clb = XRCCTRL( *this, "chkBBPlugins", wxCheckListBox );
    for( size_t i = 0; i < clb->GetCount(); ++i ) {
      if( clb->IsChecked( i ) ) {
        const PluginElementsArray& plugins = Manager::Get()->GetPluginManager()->GetPlugins();
        for( size_t n = 0; n < plugins.GetCount(); ++n ) {
          PluginElement* elem = plugins[n];
          if( !elem ) {
            continue;
          }
          if( elem->info.title == clb->GetString( i ) ) {
            bbplugins.Add( wxFileName( elem->fileName ).GetFullName() );
            break;
          }
        }
      }
    }
    cbWriteBatchBuildPlugins( bbplugins, this );
    for( size_t i = 0; i < m_PluginPanels.GetCount(); ++i ) {
      cbConfigurationPanel* panel = m_PluginPanels[i];
      panel->OnApply();
    }
  } else {
    for( size_t i = 0; i < m_PluginPanels.GetCount(); ++i ) {
      cbConfigurationPanel* panel = m_PluginPanels[i];
      panel->OnCancel();
    }
  }
  wxScrollingDialog::EndModal( retCode );
}
